<html><!-- Created using the cpp_pretty_printer from the dlib C++ library.  See http://dlib.net for updates. --><head><title>dlib C++ Library - unordered_pair.h</title></head><body bgcolor='white'><pre>
<font color='#009900'>// Copyright (C) 2010  Davis E. King (davis@dlib.net)
</font><font color='#009900'>// License: Boost Software License   See LICENSE.txt for the full license.
</font><font color='#0000FF'>#ifndef</font> DLIB_UNORDERED_PAiR_Hh_
<font color='#0000FF'>#define</font> DLIB_UNORDERED_PAiR_Hh_

<font color='#0000FF'>#include</font> "<a style='text-decoration:none' href='serialize.h.html'>serialize.h</a>"

<font color='#0000FF'>namespace</font> dlib
<b>{</b>

<font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
    <font color='#0000FF'>template</font> <font color='#5555FF'>&lt;</font>
        <font color='#0000FF'>typename</font> T
        <font color='#5555FF'>&gt;</font>
    <font color='#0000FF'>struct</font> <b><a name='unordered_pair'></a>unordered_pair</b> 
    <b>{</b>
        <font color='#009900'>/*!
            REQUIREMENTS ON T
                T must be default constructable, copyable, and comparable using
                operator &lt; and ==

            WHAT THIS OBJECT REPRESENTS
                This object is very similar to the std::pair struct except unordered_pair 
                is only capable of representing an unordered set of two items rather than 
                an ordered list of two items like std::pair.  

                This is best illustrated by example.  Suppose we have the following
                five variables:
                    std::pair&lt;int,int&gt; p1(1, 5), p2(5,1);
                    unordered_pair&lt;int&gt; up1(1,5), up2(5,1), up3(6,7);

                Then it is the case that:   
                    up1 == up2
                    up1 != up3
                    p1 != p2

               So the unordered_pair doesn't care about the order of the arguments.
               In this case, up1 and up2 are both equivalent.

        !*/</font>

        <font color='#0000FF'>typedef</font> T type;
        <font color='#0000FF'>typedef</font> T first_type;
        <font color='#0000FF'>typedef</font> T second_type;

        <font color='#0000FF'>const</font> T first;
        <font color='#0000FF'>const</font> T second;

        <b><a name='unordered_pair'></a>unordered_pair</b><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font> : first<font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>, second<font face='Lucida Console'>(</font><font face='Lucida Console'>)</font> 
        <font color='#009900'>/*!
            ensures
                - #first and #second are default initialized
        !*/</font> <b>{</b><b>}</b>

        <b><a name='unordered_pair'></a>unordered_pair</b><font face='Lucida Console'>(</font>
            <font color='#0000FF'>const</font> T<font color='#5555FF'>&amp;</font> a, 
            <font color='#0000FF'>const</font> T<font color='#5555FF'>&amp;</font> b
        <font face='Lucida Console'>)</font> :
            first<font face='Lucida Console'>(</font> a <font color='#5555FF'>&lt;</font> b ? a : b<font face='Lucida Console'>)</font>,
            second<font face='Lucida Console'>(</font>a <font color='#5555FF'>&lt;</font> b ? b : a<font face='Lucida Console'>)</font>
        <font color='#009900'>/*!
            ensures
                - #first &lt;= #second
                - #first and #second contain copies of the items a and b.
        !*/</font> <b>{</b><b>}</b>

        <font color='#0000FF'>template</font> <font color='#5555FF'>&lt;</font><font color='#0000FF'>typename</font> U<font color='#5555FF'>&gt;</font>
        <b><a name='unordered_pair'></a>unordered_pair</b> <font face='Lucida Console'>(</font>
            <font color='#0000FF'>const</font> unordered_pair <font color='#5555FF'>&lt;</font>U<font color='#5555FF'>&gt;</font><font color='#5555FF'>&amp;</font> p
        <font face='Lucida Console'>)</font> :
            first<font face='Lucida Console'>(</font>p.first<font face='Lucida Console'>)</font>,
            second<font face='Lucida Console'>(</font>p.second<font face='Lucida Console'>)</font>
        <font color='#009900'>/*!
            ensures
                - #*this is a copy of p
        !*/</font> <b>{</b><b>}</b>

        unordered_pair<font color='#5555FF'>&amp;</font> <b><a name='operator'></a>operator</b><font color='#5555FF'>=</font> <font face='Lucida Console'>(</font>
            <font color='#0000FF'>const</font> unordered_pair<font color='#5555FF'>&amp;</font> item
        <font face='Lucida Console'>)</font> 
        <font color='#009900'>/*!
            ensures
                - #*this == item
        !*/</font>
        <b>{</b>
            <font color='#0000FF'>const_cast</font><font color='#5555FF'>&lt;</font>T<font color='#5555FF'>&amp;</font><font color='#5555FF'>&gt;</font><font face='Lucida Console'>(</font>first<font face='Lucida Console'>)</font> <font color='#5555FF'>=</font> item.first;
            <font color='#0000FF'>const_cast</font><font color='#5555FF'>&lt;</font>T<font color='#5555FF'>&amp;</font><font color='#5555FF'>&gt;</font><font face='Lucida Console'>(</font>second<font face='Lucida Console'>)</font> <font color='#5555FF'>=</font> item.second;
            <font color='#0000FF'>return</font> <font color='#5555FF'>*</font><font color='#0000FF'>this</font>;
        <b>}</b>
    <b>}</b>;

<font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
    <font color='#0000FF'>template</font> <font color='#5555FF'>&lt;</font><font color='#0000FF'>typename</font> T<font color='#5555FF'>&gt;</font>
    <font color='#0000FF'><u>bool</u></font> <b><a name='operator'></a>operator</b><font color='#5555FF'>=</font><font color='#5555FF'>=</font><font face='Lucida Console'>(</font><font color='#0000FF'>const</font> unordered_pair<font color='#5555FF'>&lt;</font>T<font color='#5555FF'>&gt;</font><font color='#5555FF'>&amp;</font> a, <font color='#0000FF'>const</font> unordered_pair <font color='#5555FF'>&lt;</font>T<font color='#5555FF'>&gt;</font><font color='#5555FF'>&amp;</font> b<font face='Lucida Console'>)</font>
    <b>{</b>
        <font color='#0000FF'>return</font> a.first <font color='#5555FF'>=</font><font color='#5555FF'>=</font> b.first <font color='#5555FF'>&amp;</font><font color='#5555FF'>&amp;</font> a.second <font color='#5555FF'>=</font><font color='#5555FF'>=</font> b.second;
    <b>}</b>

    <font color='#0000FF'>template</font> <font color='#5555FF'>&lt;</font><font color='#0000FF'>typename</font> T<font color='#5555FF'>&gt;</font>
    <font color='#0000FF'><u>bool</u></font> <b><a name='operator'></a>operator</b><font color='#5555FF'>!</font><font color='#5555FF'>=</font><font face='Lucida Console'>(</font><font color='#0000FF'>const</font> unordered_pair<font color='#5555FF'>&lt;</font>T<font color='#5555FF'>&gt;</font><font color='#5555FF'>&amp;</font> a, <font color='#0000FF'>const</font> unordered_pair <font color='#5555FF'>&lt;</font>T<font color='#5555FF'>&gt;</font><font color='#5555FF'>&amp;</font> b<font face='Lucida Console'>)</font>
    <b>{</b>
        <font color='#0000FF'>return</font> <font color='#5555FF'>!</font><font face='Lucida Console'>(</font>a <font color='#5555FF'>=</font><font color='#5555FF'>=</font> b<font face='Lucida Console'>)</font>;
    <b>}</b>

    <font color='#0000FF'>template</font> <font color='#5555FF'>&lt;</font><font color='#0000FF'>typename</font> T<font color='#5555FF'>&gt;</font>
    <font color='#0000FF'><u>bool</u></font> <b><a name='operator'></a>operator</b><font color='#5555FF'>&lt;</font><font face='Lucida Console'>(</font><font color='#0000FF'>const</font> unordered_pair<font color='#5555FF'>&lt;</font>T<font color='#5555FF'>&gt;</font><font color='#5555FF'>&amp;</font> a, <font color='#0000FF'>const</font> unordered_pair<font color='#5555FF'>&lt;</font>T<font color='#5555FF'>&gt;</font><font color='#5555FF'>&amp;</font> b<font face='Lucida Console'>)</font>
    <b>{</b>
        <font color='#0000FF'>return</font> <font face='Lucida Console'>(</font>a.first <font color='#5555FF'>&lt;</font> b.first <font color='#5555FF'>|</font><font color='#5555FF'>|</font> <font face='Lucida Console'>(</font><font color='#5555FF'>!</font><font face='Lucida Console'>(</font>b.first <font color='#5555FF'>&lt;</font> a.first<font face='Lucida Console'>)</font> <font color='#5555FF'>&amp;</font><font color='#5555FF'>&amp;</font> a.second <font color='#5555FF'>&lt;</font> b.second<font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>;
    <b>}</b>

    <font color='#0000FF'>template</font> <font color='#5555FF'>&lt;</font><font color='#0000FF'>typename</font> T<font color='#5555FF'>&gt;</font>
    <font color='#0000FF'><u>bool</u></font> <b><a name='operator'></a>operator</b><font color='#5555FF'>&gt;</font><font face='Lucida Console'>(</font><font color='#0000FF'>const</font> unordered_pair<font color='#5555FF'>&lt;</font>T<font color='#5555FF'>&gt;</font><font color='#5555FF'>&amp;</font> a, <font color='#0000FF'>const</font> unordered_pair <font color='#5555FF'>&lt;</font>T<font color='#5555FF'>&gt;</font><font color='#5555FF'>&amp;</font> b<font face='Lucida Console'>)</font>
    <b>{</b>
        <font color='#0000FF'>return</font> b <font color='#5555FF'>&lt;</font> a;
    <b>}</b>

    <font color='#0000FF'>template</font> <font color='#5555FF'>&lt;</font><font color='#0000FF'>typename</font> T<font color='#5555FF'>&gt;</font>
    <font color='#0000FF'><u>bool</u></font> <b><a name='operator'></a>operator</b><font color='#5555FF'>&lt;</font><font color='#5555FF'>=</font><font face='Lucida Console'>(</font><font color='#0000FF'>const</font> unordered_pair<font color='#5555FF'>&lt;</font>T<font color='#5555FF'>&gt;</font><font color='#5555FF'>&amp;</font> a, <font color='#0000FF'>const</font> unordered_pair <font color='#5555FF'>&lt;</font>T<font color='#5555FF'>&gt;</font><font color='#5555FF'>&amp;</font> b<font face='Lucida Console'>)</font>
    <b>{</b>
        <font color='#0000FF'>return</font> <font color='#5555FF'>!</font><font face='Lucida Console'>(</font>b <font color='#5555FF'>&lt;</font> a<font face='Lucida Console'>)</font>;
    <b>}</b>

    <font color='#0000FF'>template</font> <font color='#5555FF'>&lt;</font><font color='#0000FF'>typename</font> T<font color='#5555FF'>&gt;</font>
    <font color='#0000FF'><u>bool</u></font> <b><a name='operator'></a>operator</b><font color='#5555FF'>&gt;</font><font color='#5555FF'>=</font><font face='Lucida Console'>(</font><font color='#0000FF'>const</font> unordered_pair<font color='#5555FF'>&lt;</font>T<font color='#5555FF'>&gt;</font><font color='#5555FF'>&amp;</font> a, <font color='#0000FF'>const</font> unordered_pair <font color='#5555FF'>&lt;</font>T<font color='#5555FF'>&gt;</font><font color='#5555FF'>&amp;</font> b<font face='Lucida Console'>)</font>
    <b>{</b>
        <font color='#0000FF'>return</font> <font color='#5555FF'>!</font><font face='Lucida Console'>(</font>a <font color='#5555FF'>&lt;</font> b<font face='Lucida Console'>)</font>;
    <b>}</b>

    <font color='#0000FF'>template</font> <font color='#5555FF'>&lt;</font><font color='#0000FF'>typename</font> T<font color='#5555FF'>&gt;</font>
    unordered_pair<font color='#5555FF'>&lt;</font>T<font color='#5555FF'>&gt;</font> <b><a name='make_unordered_pair'></a>make_unordered_pair</b> <font face='Lucida Console'>(</font><font color='#0000FF'>const</font> T<font color='#5555FF'>&amp;</font> a, <font color='#0000FF'>const</font> T<font color='#5555FF'>&amp;</font> b<font face='Lucida Console'>)</font>
    <b>{</b>
        <font color='#0000FF'>return</font> unordered_pair<font color='#5555FF'>&lt;</font>T<font color='#5555FF'>&gt;</font><font face='Lucida Console'>(</font>a,b<font face='Lucida Console'>)</font>;
    <b>}</b>

<font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
    <font color='#0000FF'>template</font> <font color='#5555FF'>&lt;</font><font color='#0000FF'>typename</font> T<font color='#5555FF'>&gt;</font>
    <font color='#0000FF'><u>void</u></font> <b><a name='serialize'></a>serialize</b> <font face='Lucida Console'>(</font>
        <font color='#0000FF'>const</font> unordered_pair<font color='#5555FF'>&lt;</font>T<font color='#5555FF'>&gt;</font><font color='#5555FF'>&amp;</font> item,
        std::ostream<font color='#5555FF'>&amp;</font> out
    <font face='Lucida Console'>)</font>
    <b>{</b>
        <font color='#0000FF'>try</font>
        <b>{</b> 
            <font color='#BB00BB'>serialize</font><font face='Lucida Console'>(</font>item.first,out<font face='Lucida Console'>)</font>; 
            <font color='#BB00BB'>serialize</font><font face='Lucida Console'>(</font>item.second,out<font face='Lucida Console'>)</font>; 
        <b>}</b>
        <font color='#0000FF'>catch</font> <font face='Lucida Console'>(</font>serialization_error<font color='#5555FF'>&amp;</font> e<font face='Lucida Console'>)</font>
        <b>{</b> <font color='#0000FF'>throw</font> <font color='#BB00BB'>serialization_error</font><font face='Lucida Console'>(</font>e.info <font color='#5555FF'>+</font> "<font color='#CC0000'>\n   while serializing object of type unordered_pair</font>"<font face='Lucida Console'>)</font>; <b>}</b>
    <b>}</b>

    <font color='#0000FF'>template</font> <font color='#5555FF'>&lt;</font><font color='#0000FF'>typename</font> T<font color='#5555FF'>&gt;</font>
    <font color='#0000FF'><u>void</u></font> <b><a name='deserialize'></a>deserialize</b> <font face='Lucida Console'>(</font>
        unordered_pair<font color='#5555FF'>&lt;</font>T<font color='#5555FF'>&gt;</font><font color='#5555FF'>&amp;</font> item,
        std::istream<font color='#5555FF'>&amp;</font> in 
    <font face='Lucida Console'>)</font>
    <b>{</b>
        <font color='#0000FF'>try</font>
        <b>{</b> 
            T a, b;
            <font color='#BB00BB'>deserialize</font><font face='Lucida Console'>(</font>a,in<font face='Lucida Console'>)</font>; 
            <font color='#BB00BB'>deserialize</font><font face='Lucida Console'>(</font>b,in<font face='Lucida Console'>)</font>; 
            item <font color='#5555FF'>=</font> <font color='#BB00BB'>make_unordered_pair</font><font face='Lucida Console'>(</font>a,b<font face='Lucida Console'>)</font>;
        <b>}</b>
        <font color='#0000FF'>catch</font> <font face='Lucida Console'>(</font>serialization_error<font color='#5555FF'>&amp;</font> e<font face='Lucida Console'>)</font>
        <b>{</b> <font color='#0000FF'>throw</font> <font color='#BB00BB'>serialization_error</font><font face='Lucida Console'>(</font>e.info <font color='#5555FF'>+</font> "<font color='#CC0000'>\n   while deserializing object of type unordered_pair</font>"<font face='Lucida Console'>)</font>; <b>}</b>
    <b>}</b>

<font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
<b>}</b>

<font color='#0000FF'>#endif</font> <font color='#009900'>// DLIB_UNORDERED_PAiR_Hh_
</font>

</pre></body></html>